﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;

namespace Skynet.InternetServices
{
    public class Global : System.Web.HttpApplication
    {

        private void InitializeContainer()
        {
            var ContainerService = new UnityContainer();

            var AspectPath = System.Web.HttpContext.Current.Server.MapPath("~/Config/Aspect.config");
            var AspectMap = new ExeConfigurationFileMap() { ExeConfigFilename = AspectPath };
            var AspectSection = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(AspectMap, ConfigurationUserLevel.None).GetSection("unity") as UnityConfigurationSection;
            ContainerService.LoadConfiguration(AspectSection);

            var DataAccessPath = System.Web.HttpContext.Current.Server.MapPath("~/Config/DataAccess.config");
            var DataAccessMap = new ExeConfigurationFileMap() { ExeConfigFilename = DataAccessPath };
            var DataAccessSection = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(DataAccessMap, ConfigurationUserLevel.None).GetSection("unity") as UnityConfigurationSection;
            ContainerService.LoadConfiguration(DataAccessSection);


            var BusinessPath = System.Web.HttpContext.Current.Server.MapPath("~/Config/Business.config");
            var BusinessMap = new ExeConfigurationFileMap() { ExeConfigFilename = BusinessPath };
            var BusinessSection = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(BusinessMap, ConfigurationUserLevel.None).GetSection("unity") as UnityConfigurationSection;
            ContainerService.LoadConfiguration(BusinessSection);

            var translator = ContainerService.Resolve<Infrastructure.Aspect.Services.Traslator.IEntityTranslatorService>();
            //translator.RegisterEntityTranslator(new Skynet.ServiceImplementation.Translators.ExamCategoryTranslator());
            //translator.RegisterEntityTranslator(new Skynet.ServiceImplementation.Translators.ExamCategoriesTranslator());            

            System.Web.HttpContext.Current.Application["ContainerService"] = ContainerService;


        }
        protected void Application_Start(object sender, EventArgs e)
        {
            InitializeContainer();
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}